package com.imooc.business.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
public class DateUtil extends InnerDateUtil{

    /**
     * 获取昨天时间
     * @return
     */
    public static Date getYesterday() {
        Calendar c = convertToCalendar(new Date());
        c.add(5, -1);
        return c.getTime();
    }



    /**
     * 获取一天开始时间
     * @param date
     * @return
     */
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取一天结束时间
     * @param date
     * @return
     */
    public static Date getEndOfDay(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(11, 23);
        c.set(12, 59);
        c.set(13, 59);
        c.set(14, 999);
        return c.getTime();
    }

    /**
     * 获取一周开始的一天
     * @param date
     * @return
     */
    public static Date getStartOfWeek(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.setFirstDayOfWeek(2);
        c.set(7, 2);
        c.set(11, 0);
        c.set(12, 0);
        c.set(13, 0);
        c.set(12, 0);
        c.set(14, 0);
        return c.getTime();
    }

    /**
     * 获取一周结束的一天
     * @param date
     * @return
     */
    public static Date getEndOfWeek(Date date) {
        if (date == null) {
            return null;
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.setFirstDayOfWeek(2);
        c.set(7, 1);
        c.set(11, 23);
        c.set(12, 59);
        c.set(13, 59);
        c.set(14, 999);
        return c.getTime();
    }

    /**
     * 获取一个月开始的一天
     * @param date
     * @return
     */
    public static Date getStartOfMonth(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(5, 1);
        c.set(11, 0);
        c.set(12, 0);
        c.set(13, 0);
        c.set(14, 0);
        return c.getTime();
    }

    /**
     * 获取一个月结束的一天
     * @param date
     * @return
     */
    public static Date getEndOfMonth(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(5, c.getActualMaximum(5));
        c.set(11, 23);
        c.set(12, 59);
        c.set(13, 59);
        c.set(14, 999);
        return c.getTime();
    }

    /**
     * 获取一个学期开始的一天
     * @param date
     * @return
     */
    public static Date getStartOfSemester(Date date) {
        String year = getYear(date);
        String semester = "1".equals(getSeasonType(date)) ? formatDate(getSemesterStartDate(date), "yyyy-MM-dd") : year.concat("-09-01");
        return parseDate(semester, "yyyy-MM-dd");
    }


    /**
     * 获取一个学期结束的一天
     * @param date
     * @return
     */
    public static Date getEndOfSemester(Date date) {
        String year = getYear(date);
        String semester = "1".equals(getSeasonType(date)) ? String.valueOf(Integer.parseInt(year) + 1).concat("-08-31") : formatDate(getSemesterStartDate(date), "yyyy-MM-dd");
        return parseDate(semester, "yyyy-MM-dd");
    }

    /**
     * 返回年份
     * @param date
     * @return
     */
    public static String getYear(Date date) {
        return formatDate(date, "yyyy");
    }

    /**
     * 获取一个学年开始的一天
     * @param date
     * @return
     */
    public static Date getStartOfSchoolYear(Date date) {
        Date schoolYear = parseDate(getYear(date).concat("-09-01"), "yyyy-MM-dd");
        return schoolYear;
    }

    /**
     * 获取一个学年结束的一天
     * @param date
     * @return
     */
    public static Date getEndOfSchoolYear(Date date) {
        return parseDate(String.valueOf(Integer.parseInt(getYear(date)) + 1).concat("-08-31"), "yyyy-MM-dd");
    }

    public static void main(String[] args) {
        Date date = new Date();

        Date startOfDay = getStartOfDay(date);
        Date dayEndTime = getEndOfDay(date);
        System.out.println("orgin date: "+date);
        System.out.println(startOfDay);
        System.out.println(dayEndTime);

        System.out.println("week: ");
        Date startOfWeek = getStartOfWeek(date);
        Date endOfWeek = getEndOfWeek(date);
        System.out.println(startOfWeek);
        System.out.println(endOfWeek);

        System.out.println("month: ");
        Date startOfMonth = getStartOfMonth(date);
        Date endOfMonth = getEndOfMonth(date);
        System.out.println(startOfMonth);
        System.out.println(endOfMonth);

        System.out.println("semester: ");
        Date startOfSemester = getStartOfSemester(date);
        Date endOfSemester = getEndOfSemester(date);
        System.out.println(startOfSemester);
        System.out.println(endOfSemester);

        System.out.println("schoolYear: ");
        Date startOfSchoolYear = getStartOfSchoolYear(date);
        Date endOfSchoolYear = getEndOfSchoolYear(date);
        System.out.println(startOfSchoolYear);
        System.out.println(endOfSchoolYear);

        System.out.println(getYesterday());
    }

}
